﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElfTyping.States.GameState.Entities;
using ElfTyping.General;
using Microsoft.Xna.Framework;

namespace ElfTyping.States.GameState
{
    public class HealthBar
    {
        const int Width = 50;
        const int Height = 10;

        public Color BackgroundColor = Color.Black;
        public Color HealthColor = Color.LightBlue;
        public Color HurtColor = Color.DarkRed;

        HealthEntity entity;

        public HealthBar(HealthEntity healthEntity)
        {
            entity = healthEntity;
        }

        public void Draw(Camera camera)
        {
            float bottomY = entity.DrawBox.Bottom + Height;
            float leftX = entity.CenterPosition.X - Width/2f;

            float healthPercent = MathHelper.Clamp((float)entity.CurrentHealth / (float)entity.MaximumHealth, 0, 1);
            float hurtStartX = leftX + 1 + healthPercent * (Width - 2);
            float hurtLength = (1 - healthPercent) * (Width - 2);

            
            Rectangle bgRectangle = new Rectangle((int)leftX, (int)bottomY, Width, Height);
            Rectangle healthRectangle = new Rectangle((int)leftX + 1, (int)bottomY + 1, Width - 2 - (int)hurtLength, Height - 2);
            Rectangle hurtRectangle = new Rectangle((int)hurtStartX , (int)bottomY+ + 1, (int)hurtLength, Height - 2);

            camera.DrawRectangle(bgRectangle, BackgroundColor);
            camera.DrawRectangle(healthRectangle, HealthColor);
            camera.DrawRectangle(hurtRectangle, HurtColor);
            
            for (int quartile = 1; quartile <= 3; quartile++)
            {
                Rectangle quartileDivider = new Rectangle((int)leftX + Width * quartile / 4, (int)bottomY + 1, 1, Height - 2);
                Color dividerColor = (healthPercent <= quartile / 4f) ? HealthColor : HurtColor;

                camera.DrawRectangle(quartileDivider, dividerColor);
            }

            //camera.MakeText(entity.CurrentHealth.ToString())
            //    .WithCenteredPosition(entity.CenterPosition.X, bottomY + 25)
            //    .Draw();
        }
    }
}
